<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/3
 * Time: 15:04
 */

namespace app\api\controller;
use app\model\Address as SelfModel;
use think\Request;


class Address extends Base
{
    /**
     * 地址列表
     */
    public function index(Request $request)
    {

        $data = SelfModel::where('member_id',$request->member_id)->order(['status' => 'asc','id'=>'desc'])->select();
        return ['code' => 200, 'msg' => '成功', 'data' => $data];
    }

    /**
     * 订单我的地址
     */
    public function my_index(Request $request)
    {
        $data = SelfModel::where('member_id',$request->member_id)->order(['status' => 'asc','id'=>'desc'])->find();
        if(empty($data)){
           // $data['mobile'] =  $data['name'] =    $data['address'] = '';
        }
        return ['code' => 200, 'msg' => '成功', 'data' => $data];
    }

    /**
     * 添加地址
     */
    public function create(Request $request)
    {


        $address = SelfModel::where('member_id', $request->member_id)->find();
        if(empty($address)){
            $request->status = 1;
        }

        //if (empty($address) && $param['status'] == 2) return j_encrypt(['code' => 400, 'message' => '您还没有默认的地址,请选择为默认地址']);
        $default_address = SelfModel::where('member_id',$request->member_id)->where('status', 1)->find(); //默认地址已经存在
        if($request->status == 1){
            SelfModel::where('member_id', $request->member_id)->where('status', 1)->update(['status'=>2]);
        }

        $ret = SelfModel::create($request->param);
        if ($ret) {
            return ['code' => 200, 'msg' => '地址添加成功'];
        }
        return ['code' => 400, 'msg' => '网络繁忙'];

    }

    /**
     * 修改地址
     */
    public function update(Request $request)
    {

//        $result = $this->validate($param, 'Address.update');
//        if (true !== $result) return j_encrypt(['code' => 400, 'message' => $result]);//普通验证

//        if ($param['status'] == 2) { //当前地址改成普通地址，而其他还没有设置默认地址;
//            $find = SelfModel::where(['member_id' => $param['member_id'], 'status' => 1, 'id' => ['neq', $param['id']]])->find();//当前会员非当前id 等于1是有默认地址
//            if (empty($find)) return j_encrypt(['code' => 400, 'message' => '我的地址里没有默认地址,请设置一个默认地址']);//给默认地址删除了，在修改的时候就必须得选择一个默认地址状态1
//        }
//        if($param['status'] == 1){ //改成默认地址 那其他的就应该不是默认了
//            SelfModel::where('member_id', $param['member_id'])->where('status', 1)->setField('status', 2); //把默认地址修改成普通地址 然后这个改成默认地址
//        }

        $info = SelfModel::find($request->id);

        $ret = $info->save($request->param);

        if ($ret !== false) {
            return ['code' => 200, 'msg' => '修改成功'];
        }
        return ['code' => 401, 'msg' => '网络繁忙'];

    }

    /**
     * 删除非默认地址
     */
    public function destory(Request $request)
    {

        $is_default = SelfModel::where('member_id', $request->member_id)->where('id',$request->id)->value('status');
        if ($is_default == 1) {
            return ['code' => 400, 'msg' => '默认地址不能删除,可把其他地址设置为默认再删除'];
        }
        $ret = SelfModel::destroy($request->id);
        if ($ret) {
            return ['code' => 200, 'msg' => '删除成功'];
        }
        return ['code' => 401, 'msg' => '网络繁忙'];

    }

    /**
     * 地址详情接口
     * @return \think\response\Json
     */
    public function view(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->find();
        return ['code' => 200, 'msg' => '地址详情', 'data' => $data];
    }

    /**
     * 默认地址
     */
    public function default_status()
    {
        $param = j_decrypt();
        SelfModel::where('member_id', $param['member_id'])->where('status', 1)->setField('status', 2); //把默认地址修改成普通地址2
        $ret = SelfModel::where('member_id', $param['member_id'])->where('id', $param['id'])->setField('status', 1); //当前地址改完默认
        if ($ret !== false) {
            return j_encrypt(['code' => 200, 'message' => '默认地址设置成功']);
        }
        return j_encrypt(['code' => 401, 'message' => '网络繁忙']);
    }
}